<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">IQC任务编号：</label>
                                        <div class="layui-input-inline">
                                            <input name="code" class="layui-input" type="text" placeholder="请输入任务编号" />
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">单据编号：</label>
                                        <div class="layui-input-inline">
                                            <input name="orderCode" class="layui-input" type="text" placeholder="请输入单据编号" />
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">来源：</label>
                                        <div class="layui-input-inline">
                                            <select name="sourceType">
                                                <option value="">请选择来源</option>
                                                <option value="1">按采购订单</option>
                                                <option value="2">按供应商发货物流</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">质检分类：</label>
                                        <div class="layui-input-inline">
                                            <select id="qualityTestingCategoryIdSearch" name="qualityTestingCategoryId" lay-filter="qualityTestingCategoryId" class="common-select-search"></select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">供应商：</label>
                                        <div class="layui-input-inline">
                                            <select id="supplierId" name="supplierId" lay-filter="supplierId" class="common-select-search"></select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">创建时间：</label>
                                        <div class="layui-input-inline">
                                            <input name="createTime" id="createTime" class="layui-input date-icon" type="text" placeholder="请选择创建时间" style="width: 200px" readonly />
                                        </div>
                                        <input name="startCreateTime" id="sCreateTime" type="hidden" />
                                        <input name="endCreateTime" id="eCreateTime" type="hidden" />
                                    </div>
                                </div>
                                <div class="query-rt">
                                    <div class="layui-inline">
                                        <button type="button" class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit type="button">重置</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="lay-btn-rows">
                        <div class="lay-btn-box">
                            <div class="lay-btn-lt">
                                <button id="purQualityTaskBtnAdd" class="layui-btn icon-btn color-reseda" type="button">添加IQC质检任务</button>
                            </div>
                            <div class="lay-btn-rt"></div>
                        </div>
                    </div>
                </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="purQualityTaskTable" lay-filter="purQualityTaskTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purQualityTaskTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
    <!-- {{# if( d.status=="2"){ }} -->
    <a class="layui-btn layui-btn-xs" lay-event="determine">认领</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="close">作废</a>
    <!-- {{# } }} -->
</script>

<!-- js部分 -->
<script>
    layui.use(["layer", "form", "table", "admin", "laydate"], async function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var laydate = layui.laydate;

        laydate.render({
            elem: "#createTime",
            type: "date",
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                    $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
                } else {
                    $("input[name='startCreateTime']").val("");
                    $("input[name='endCreateTime']").val("");
                }
            },
        });
        await admin.initDataSelectPro("production/qualityTesting/admin/qualityTestingCategory/list", {
            limit: '999'
        }, "qualityTestingCategoryIdSearch", "质检分类");
        await admin.initDataSelectPro("enterprise/purchase/admin/supplierInfoPass/list", {
            limit: '999'
        }, "supplierId", "供应商", null, 'corporateName');

        form.render("select");

        // 渲染表格
        var insTb = table.render({
            elem: "#purQualityTaskTable",
            url: config.base + "enterprise/purchase/admin/purQualityTask/list",
            page: true,
            cellMinWidth: 100,
            cols: [
                [
                    { type: "numbers", title: "序号" },
                    { field: "code", title: "IQC任务编号",width: 150  },
                    { field: "sourceTypeName", title: "任务来源" },
                    { field: "orderCode", title: "单据编号", width: 150},
                    { field: "skuName", title: "物料名称" },
                    { field: "skuCode", title: "物料编码" },
                    { field: "qualityTestingCategoryName", title: "质检分类" },
                    { field: "qualityTestingTypeName", title: "质检性质" },
                    { field: "qcQualityTypeName", title: "质检类型" },
                    { field: "submissionQuantity", title: "送检数量", },
                    { field: "statusName", title: "状态", },
                    { field: "createTime", title: "创建时间"},
                    {
                        align: "center",
                        toolbar: "#purQualityTaskTableBar",
                        title: "操作",
                        fixed: "right",
                        width: 200,
                    },
                ],
            ],
            done: function (res, curr, count) {
                //移除按钮
            },
        });
        // 工具条点击事件
        table.on("tool(purQualityTaskTable)", function (obj) {
            let data = obj.data;
            if (obj.event === "details") {
                showQualityInspectionTaskModel(data, "details");
            } else if (obj.event === "close") {
                closeTask(data.id);
            } else if (obj.event === "determine") {
                showQualityInspectionTaskModel(data, "determine")
            }
        });

        //监听排序
        table.on("sort(purQualityTaskTable)", function (obj) {
            table.reload("purQualityTaskTable", {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type,
                },
            });
        });

        // 搜索
        form.on("submit(formSubSearchLog)", function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
        });

        // 重置搜索
        form.on("submit(resets)", function (data) {
            insTb.reload(
                { where: admin.resetSearch(data.field), page: { curr: 1 } },
                "data"
            );
            return false;
        });

        // 添加按钮点击事件
        $("#purQualityTaskBtnAdd").click(function () {
            showQualityInspectionTaskModel();
        });

        // 添加质检任务弹窗 && 质检任务详情弹窗
        function showQualityInspectionTaskModel(data, _type) {
            //如果是详情 或者是 修改
            if (data) {
                data.type = _type;
                admin.putTempData("workOrderQuaTaskInfoData", data);
            } else {
                admin.putTempData("workOrderQuaTaskInfoData", null);
            }
            if (data) {
                if (_type === "determine") {
                    admin.formOpen({
                        type: 1,
                        area: ["80%", "85%"],
                        style: "font-size: 20px;",
                        title: "IQC质检任务认领",
                        path: "components/purchase/purQuality/purQualityTaskForm.html",
                        finish: function () {
                            table.reload(insTb, {});
                        },
                    });
                } else if (_type === "details") {
                    admin.formOpen({
                        type: 1,
                        area: ["80%", "85%"],
                        style: "font-size: 20px;",
                        title: "质检任务详情",
                        path: "components/purchase/purQuality/purQualityTaskDetailForm.html",
                        finish: function () {
                            table.reload(insTb, {});
                        },
                    });
                }
            } else {
                admin.formOpen({
                    type: 1,
                    area: ["80%", "85%"],
                    style: "font-size: 20px;",
                    title: "添加IQC质检任务",
                    path: "components/purchase/purQuality/purQualityTaskForm.html",
                    finish: function () {
                        table.reload(insTb, {});
                    },
                });
            }
        }

        // 作废任务
        function closeTask(id) {
            layer.confirm(
                "确定要作废吗？",
                {
                    offset: "65px",
                    skin: "layui-layer-admin",
                },
                function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req(
                        "enterprise/purchase/admin/purQualityTask/voidBy/" + id, {},
                        function (res) {
                            layer.closeAll("loading");
                            if (res.code == 200) {
                                layer.msg(res.msg, { icon: 1 });
                                table.reload("purQualityTaskTable");
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        },
                        "POST"
                    );
                }
            );
        }
    });
</script>
